package com.earnings.okhttputils.utils.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.Listener.NetworkRequest;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.YUrlAddress;
import com.earnings.okhttputils.utils.bean.EarningsData2;
import com.earnings.okhttputils.utils.bean.YData;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.activity.DetailActivity;
import com.earnings.okhttputils.utils.ui.activity.LoveDetailActivity;
import com.earnings.okhttputils.utils.ui.activity.MyLoveActivity;
import com.earnings.okhttputils.utils.ui.activity.UpGreenActivity;
import com.earnings.okhttputils.utils.ui.activity.common.RecordActivity;
import com.earnings.okhttputils.utils.ui.activity.user.UserHelpActivity;

import java.util.Map;

/**
 * 收益
 * Created by Administrator on 2017/4/21 0021.
 */

@SuppressLint("ValidFragment")
public class EarningsFragment extends BaseFragment implements NetworkRequest {
    private EarningsData2 mEarningsData;
    //APP类型 0为百世商城，1为招财汇宝
    private int type = -1;
    //    private EarningsViewBinding mEarningsViewBinding;
    private TextView my_love_tv2, power_tv2, my_love_tv, power_tv, subsidy_tv, zrsubsidy_tv, money_tv;

    public EarningsFragment() {

    }

    public EarningsFragment(int type) {
        this.type = type;
        //设置APP类型
        if (type == YData.APP_BSSC) {
            YData.app_type = YData.APP_BSSC;
        }
        if (type == YData.APP_ZCHB) {
            YData.app_type = YData.APP_ZCHB;
        }

    }

    protected void init() {
        mView.findViewById(R.id.up_view).setOnClickListener(this);
        mView.findViewById(R.id.mylove_view).setOnClickListener(this);
        mView.findViewById(R.id.mylove_view2).setOnClickListener(this);
        mView.findViewById(R.id.earnings_view).setOnClickListener(this);
        mView.findViewById(R.id.love_view).setOnClickListener(this);
        mView.findViewById(R.id.more_bt).setOnClickListener(this);
        mView.findViewById(R.id.consumptiondetail_view).setOnClickListener(this);
        mView.findViewById(R.id.zraxz_view).setOnClickListener(this);
        my_love_tv2 = (TextView) mView.findViewById(R.id.my_love_tv2);
        zrsubsidy_tv = (TextView) mView.findViewById(R.id.zrsubsidy_tv);
        my_love_tv = (TextView) mView.findViewById(R.id.my_love_tv);
        power_tv = (TextView) mView.findViewById(R.id.power_tv);
        subsidy_tv = (TextView) mView.findViewById(R.id.subsidy_tv);
        power_tv2 = (TextView) mView.findViewById(R.id.power_tv2);
        money_tv = (TextView) mView.findViewById(R.id.money_tv);

//        mEarningsViewBinding = new EarningsViewBinding(mView);
//        //根据APP类型切换皮肤
//        mEarningsViewBinding.greycrystal(type);
//        mEarningsViewBinding.setOnClickListener(this);

    }

    @Override
    protected int onCreateLayout() {
        return R.layout.earnings_layout;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            EarningRequest(null, this);
        }
    }


    //收益页面请求数据
    public void EarningRequest(Map<String, String> map, final NetworkRequest request) {
        EarningRequest(null, map, request);
    }

    private String  yesterdayDynamicLove,basics;

    //收益页面请求数据
    public void EarningRequest(final Context context, Map<String, String> map, final NetworkRequest request) {
        showProgress();
        HttpMap map1 = new HttpMap(getActivity());
        OkHttpUtils.post().url(YUrlAddress.EARNINGS_URL2).params(map1).build().execute(new HttpObjectCallback<EarningsData2>(mContext) {

            @Override
            public void onSuccess(EarningsData2 data, String msg) {
//                private String richget;//0,                                                                      //累计补贴
//                private String green;//5,                                                                       //绿心
//                private String red;//0,                                                                           //红心
//                private String blue;//0,                                                                         //蓝心
//                private String richstay;//0,                                                                    //待补贴财币
//                private String bluesleep;//0,                                                                 //休眠蓝心
//                private String yesterday_rich;//0,                                                         //昨日获得补贴
//                private String dynamicLove;//123256,                                                 //动态爱心数
//                private String yesterdayDynamicLove;//1.123456,                              //昨日动态爱心值
//                private String staticLove;//123456,                                                      //静态爱心数
//                private String staticLoveValue;//0.06                                                   //静态爱心值
                //个人动力值 basics
                dismissProgress();
                mEarningsData=data;
                yesterdayDynamicLove=data.getYesterdayDynamicLove();
                basics=data.getBasics();
                my_love_tv2.setText(data.getDynamicLove());
                //个人动力值
                power_tv2.setText(basics);
                my_love_tv.setText(data.getStaticLove());
                power_tv.setText(data.getStaticLoveValue());
                subsidy_tv.setText(data.getRichget());
                zrsubsidy_tv.setText(data.getYesterday_rich());
                money_tv.setText(data.getRichstay());


            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });
    }


//        HttpMap map1 = new HttpMap(getActivity());
//        OkHttpUtils.post().url(YUrlAddress.EARNINGS_URL).params(map1).build().execute(context, new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                dismissProgress();
//                Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
//                if (mContext == null) Log.e("收益页面请求异常 =", e.toString());
//                request.RequestError(e);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                dismissProgress();
//                if (context == null) Log.e("收益页面请求成功 =", response);
//
//                request.RequestOk(response);
//                try {
//                    JSONObject jsonObject = new JSONObject(response);
//                    if (jsonObject.getInt("status") == 1) {
//                        //           mEarningsViewBinding.setViewClick(true);
//
//                        JSONObject datajsb = jsonObject.getJSONObject("result");
//                        //累计补贴
//                        richget = datajsb.getString("richget");
//                        //绿心
//                        String green = datajsb.getString("green");
//                        //待补贴财币
//                        String richstay = datajsb.getString("richstay");
//                        //红心
//                        String red = datajsb.getString("red");
//                        //蓝心
//                        String blue = datajsb.getString("blue");
//                        //休眠蓝心
//                        String bluesleep = datajsb.getString("bluesleep");
//                        //个人爱心值
//                        String love = datajsb.getString("love");
//                        //个人动力值
//                        String power = datajsb.getString("power");
//                        //全国动力值
//                        String allpower = datajsb.getString("allpower");
//                        //昨日获得补贴值
//                        String yesterday_rich = datajsb.getString("yesterday_rich");
//                        //全国爱心活跃数
//                        String allloveactive = datajsb.getString("allloveactive");
//                        //全国总消费额
//                        String allcost = datajsb.getString("allcost");
//                        //个人基础动力值
//                        basics = datajsb.getString("basics");
//
//                        String alllove = datajsb.getString("alllove");
//                        mEarningsData = new EarningsData(richget, green, red, blue, bluesleep, love, power, allpower, yesterday_rich, allloveactive, allcost, alllove, richstay, basics);
//
//                        money_tv.setText(richstay);
//                        //更新用户信息
////                        mEarningsViewBinding.setViewData(mEarningsData);
//                        //           mEarningsViewBinding.powerDialog.setValue(mEarningsData);
////                        "richget": "0.00",//累计补贴
////                                "green": "11545472.00", 	//绿心
////                                "red": "5772736.00", //红心
////                                "blue": "5772736.00",//蓝心
////                                "bluesleep": "0.00",//休眠蓝心
////                                "power": "0.0000",//个人动力值
////                                "richstay": "454.00",
////                                "love": 23090944,//个人爱心值
////                                "allpower": "1.4126",
////                                "alllove": "37.5003",
////                                "yesterday_rich": "99999999.99",//昨日获得补贴值
////                                "allloveactive": 2147483560,//全国爱心活跃数
////                                "allcost": "668074965426.00"//全国总消费额
//
//                    } else {
//                        //     mEarningsViewBinding.setViewClick(false);
//                        ToastUtils.showToast(getContext(), jsonObject.getString("msg"));
//                    }
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });
//        //     http2();



//    private void http2() {
//        showProgress();
//        HttpMap httpMap = new HttpMap(mContext);
//        OkHttpUtils.post().url(YUrlAddress.EARNINGS_URL2).params(httpMap).build().execute(new HttpObjectCallback<EarningsData2>(mContext) {
//            @Override
//            public void onSuccess(EarningsData2 data, String msg) {
//                dismissProgress();
////                "richget":0,                                                                      //累计补贴
////                        "richstay":0,                                                                    //待补贴财币
////                        "yesterday_rich":0,                                                         //昨日获得补贴
////                        "dynamicLove":123256,                                                 //动态爱心数
////                        "yesterdayDynamicLove":1.123456,                              //昨日动态爱心值
////                        "staticLove":123456,                                                      //静态爱心数
////                        "staticLoveValue":0.06                                                   //静态爱心值
//                yesterdayDynamicLove = data.getYesterdayDynamicLove();
//                my_love_tv2.setText(data.getDynamicLove());
//                power_tv2.setText(data.getYesterdayDynamicLove());
//                my_love_tv.setText(data.getStaticLove());
//                power_tv.setText(data.getStaticLoveValue());
//                subsidy_tv.setText(data.getRichget());
//                zrsubsidy_tv.setText(data.getYesterday_rich());
//            }
//
//            @Override
//            public void onFail(int code, String msg) {
//                dismissProgress();
//            }
//        });
//    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.up_view) {
            skip(UpGreenActivity.class);
        }
        if (v.getId() == R.id.mylove_view) {
            if (mEarningsData == null) {
                ToastUtils.showToast(mContext, "网络异常");
                return;
            }
            mIntent.putExtra("data", mEarningsData);
            skip(MyLoveActivity.class);
        }
        if (v.getId() == R.id.mylove_view2) {
            if (mEarningsData == null) {
                ToastUtils.showToast(mContext, "网络异常");
                return;
            }
            mIntent.putExtra("data", mEarningsData);
            skip(MyLoveActivity.class);
        }
        //补贴明细
        if (v.getId() == R.id.earnings_view) {
            skip(DetailActivity.class);
        }
        //爱心明细
        if (v.getId() == R.id.love_view) {
            skip(LoveDetailActivity.class);
        }
        //了解更多
        if (v.getId() == R.id.more_bt) {
            skip(UserHelpActivity.class);
        }
        //账单
        if (v.getId() == R.id.consumptiondetail_view) {
            skip(RecordActivity.class);
        }
        //个人爱心值
        if (v.getId() == R.id.zraxz_view) {
//            if (basics == null || yesterdayDynamicLove == null) return;
//            mIntent.putExtra("data1", yesterdayDynamicLove);
//            mIntent.putExtra("data2", basics);
//            skip(MyLove2Activity.class);
        }
    }

    @Override
    public void RequestOk(String response) {

    }

    @Override
    public void RequestError(Exception e) {

    }

}
